﻿using GameServer.Database;
using GameServer.InventorySystem;
using GameServer.Mgr;
using GameServer.Network;
using GameServer.Service;
using Google.Protobuf;
using Proto;
using Serilog;
using Summer;
using Summer.Network;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Numerics;
using System.Runtime.ConstrainedExecution;
using System.Text;
using System.Threading.Tasks;

namespace GameServer.Model
{
    //角色
    public class Character : Actor
    {
        //当前角色的客户端连接
        public Connection conn;
        public DbCharacter Data;

        //背包
        public Inventory knapsack;
        public Inventory wareKnapsack;
        public CharacterMessage Define { get; set; }=new CharacterMessage();

        public Battle.Attribute attribute { get; set; } = new Battle.Attribute();

        public SkillManager SkillMgr { get; set; }

        public int jobId;

        public int Level;
        public int Gold;
        public int Exp;
        public int SpaceID;


        public Character(int id, Vector3Int position, Vector3Int direction,int EntitySpeed, DbCharacter Define) : base(EntityType.Character, id, position, direction, EntitySpeed)
        {
            this.Define.hp = Define.Hp;
            this.Define.mp = Define.Mp;
            this.Define.attack = Define.Attack;
            this.Define.magic = Define.Magic;
            this.Define.defense = Define.Defense;
            this.Define.magicdefense = Define.MagicDefense;
            this.Define.baoji = Define.Baoji;
            this.Define.baojidefense = Define.BaojiDefense;
            this.Define.speed = Define.Speed;
            this.Define.MaxHP = Define.MaxHp;
            this.Define.MaxMp = Define.MaxMp;
            this.Level=Define.Level;
            this.SpaceID = Define.SpaceId;
            this.attribute.Init(this.Define,Define.Level);

            Name = Define.Name;

            this.jobId = Define.JobId;

            this.SkillMgr = new(this);
            this.knapsack = new Inventory(this);
            this.wareKnapsack = new Inventory(this);
            this.knapsack.Init(Define.Knapsack);
            this.wareKnapsack.Init(Define.WareKnapsack);

            this.Define.knapsackInfo = knapsack.InventoryInfo;
            this.Define.warehouseInfo = wareKnapsack.InventoryInfo;
        }

        public void SetCharacterRoleInfo(GameEntityRoleInfoMessage gameEntityRoleInfo)
        {
            this.Define.hp = gameEntityRoleInfo.Hp;
            this.Level = gameEntityRoleInfo.Level;
            this.Gold = (int)gameEntityRoleInfo.Gold;
            this.Exp = (int)gameEntityRoleInfo.Exp;
            this.Define.mp= (int)gameEntityRoleInfo.Mp;
            this.Define.attack= (int)gameEntityRoleInfo.Attack;
            this.Define.defense= (int)gameEntityRoleInfo.Defense;
            this.Define.magicdefense = gameEntityRoleInfo.Magicdefense;
            this.Define.magic = gameEntityRoleInfo.Magic;
            this.Define.speed = gameEntityRoleInfo.Speed;
            this.Define.baoji = gameEntityRoleInfo.Baoji;
            this.Define.baojidefense = gameEntityRoleInfo.Baojidefense;
            this.SpaceID = gameEntityRoleInfo.SpaceId;
            this.Define.MaxHP = gameEntityRoleInfo.Maxhp;
            this.Define.MaxMp = gameEntityRoleInfo.Maxmp;
            Data.Hp = gameEntityRoleInfo.Hp;
            Data.Level = gameEntityRoleInfo.Level;
            Data.Gold = gameEntityRoleInfo.Gold;
            Data.Exp = (int)gameEntityRoleInfo.Exp;
            Data.Mp = gameEntityRoleInfo.Mp;
            Data.Attack = gameEntityRoleInfo.Attack;
            Data.Defense = gameEntityRoleInfo.Defense;
            Data.Magic = gameEntityRoleInfo.Magic;
            Data.MagicDefense = gameEntityRoleInfo.Magicdefense;
            Data.Speed = gameEntityRoleInfo.Speed;
            Data.Baoji = gameEntityRoleInfo.Baoji;
            Data.BaojiDefense = gameEntityRoleInfo.Baojidefense;
            Data.SpaceId = gameEntityRoleInfo.SpaceId;
            Data.MaxHp = gameEntityRoleInfo.Maxhp;
            Data.MaxMp = gameEntityRoleInfo.Maxmp;
            Data.Knapsack = knapsack.InventoryInfo.ToByteArray();
            Data.WareKnapsack=wareKnapsack.InventoryInfo.ToByteArray();
        }
        public virtual void TelportSpace(Connection conn,Space _space, Vector3Int pos, Vector3Int dir = new())
        {
            //if (this is not Character chr) return;
            if (_space != Space)
            {
                //1.退出当前场景
                Space.EntityLeave(this);
                //2.设置坐标和方向
                this.Position = pos;
                this.Direction = dir;
                var player = conn.Get<Session>().DbPlayer;
                var dbRole = Db.fsql.Select<DbCharacter>().Where(t => t.Id == conn.Get<Session>().Character.entityId).Where(t => t.PlayerId == player.Id).First();
                
                Character character = conn.Get<Session>().Character;
                character.Data = dbRole;
                Data.Knapsack = this.knapsack.InventoryInfo.ToByteArray();
                Data.WareKnapsack=this.wareKnapsack.InventoryInfo.ToByteArray();
                character.Data.SpaceId=_space.Id;
                NCharacter nCharacter = new NCharacter
                {
                    Id = dbRole.Id,
                    TypeId = dbRole.JobId,
                    EntityId = entityId,
                    Name = dbRole.Name,
                    Level = dbRole.Level,
                    Exp = dbRole.Exp,
                    SpaceId = _space.Id,
                    Gold = dbRole.Gold,
                    Hp = dbRole.Hp,
                    Mp = dbRole.Mp,
                    Attack = dbRole.Attack,
                    Defense = dbRole.Defense,
                    Magic = dbRole.Magic,
                    Speed = dbRole.Speed,
                    Magicdefense = dbRole.MagicDefense,
                    Baoji = dbRole.Baoji,
                    Baojidefense = dbRole.BaojiDefense,
                    Maxhp=dbRole.MaxHp,
                    Maxmp=dbRole.MaxMp,
                    KnapsackInfo = character.Define.knapsackInfo
                };
                GameEnterResponse resp = new GameEnterResponse();
                resp.Success = true;
                resp.Entity = character.GetData();
                resp.Character = nCharacter;
                conn.Send(resp);
                //将新角色加入到地图
                var space = SpaceService.Instance.GetSpace(_space.Id); //新手村
                //3.进入新场景
                _space.EntityEnter(conn, character, nCharacter);
            }
            else
            {
                _space.Telport(this, pos, dir);
            }


        }

        public void Revive(int CharacterId)
        {
            var player = conn.Get<Session>().DbPlayer;
            var dbRole = Db.fsql.Select<DbCharacter>().Where(t => t.Id == CharacterId).Where(t => t.PlayerId == player.Id).First();
            Data.Hp = DataManager.Instance.CharacterMessageDict[jobId].hp / 2;
            Data.Level = dbRole.Level;
            Data.Gold = dbRole.Gold / 2;
            Data.Exp = (int)dbRole.Exp;
            Data.Mp = DataManager.Instance.CharacterMessageDict[jobId].mp / 2;
            Data.Attack = dbRole.Attack;
            Data.Defense = dbRole.Defense;
            Data.Magic = dbRole.Magic;
            Data.MagicDefense = dbRole.MagicDefense;
            Data.Speed = dbRole.Speed;
            Data.Baoji = dbRole.Baoji;
            Data.BaojiDefense = dbRole.BaojiDefense;
            Data.SpaceId = dbRole.SpaceId;
            Data.MaxHp=dbRole.MaxHp;
            Data.MaxMp=dbRole.MaxMp;
        }

        public override void Update()
        {
            this.SkillMgr.Update();
        }
    }
}
